package com.weaver.seconddev.tl.workflow;

/**
 * E9原始类路径:tl.workflow.ParagraphAction
 * JAVA替换说明文档:https://ncnb71xwgeda.feishu.cn/wiki/JLsjwxK8xiHuPIkdu0Dc27Rlnaf?from=from_copylink
 */
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
import java.lang.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import com.weaver.verupgrade.workflow.request.RequestManager;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.common.base.entity.result.WeaResult;
import org.springframework.beans.factory.annotation.Autowired;
import com.weaver.verupgrade.soa.workflow.request.RequestInfo;
import java.util.Map;
import java.util.HashMap;
import org.apache.commons.lang3.StringUtils;
import com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

/**
 * @author ：YunFang
 * @date ：2022/4/7 12:25
 * @description：根据自定义多选赋值下拉框
 * @modified By：
 * @version: 1.0
 */
@org.springframework.stereotype.Service("tl_workflow_ParagraphAction")
public class ParagraphAction implements Action, EsbServerlessRpcRemoteInterface {

    // 对应E9的请求信息对象
    @Autowired
    private RequestInfo requestInfoTemp;

    public static final org.slf4j.Logger logger_5b2277ab = LoggerFactory.getLogger(ParagraphAction.class);


    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        Map<String, Object> weaverResultMap = new HashMap<>();
        Long requestIdTemp = Long.parseLong(String.valueOf(requestInfoMap.getOrDefault("requestid", -1)));
        Long userIdTemp = Long.parseLong(String.valueOf(requestInfoMap.getOrDefault("userid", -1)));
        RequestInfo info = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        // TODO 返回值:requestid 存在类型转换风险 e9类型:java.lang.String e9真实类型: int  e10类型:java.lang.String e10真实类型: long
        String requestid = info.getRequestid();
        String tableName = info.getRequestManager().getBillTableName();
       // String sql = "select tpnr from " + tableName + " where requestid=" + requestid;
        String sql = "SELECT "+tableName+".tpnr AS tpnr\n" +
                "FROM (\n" +
                "\tSELECT b.*, a.requestid\n" +
                "\tFROM e10_core_business.wfc_form_data a, e10_core_business."+tableName+" b\n" +
                "\tWHERE a.dataid = b.form_data_id\n" +
                "\t\tAND a.delete_type = 0\n" +
                "\t\tAND a.tenant_key = 'tl2k2r96ty'\n" +
                ") formtable_main_16\n" +
                "WHERE "+tableName+".requestid = '"+requestid+"'\n" +
                "\tAND "+tableName+".delete_type = 0\n" +
                "\tAND "+tableName+".tenant_key = 'tl2k2r96ty'";
        logger_5b2277ab.info(String.valueOf("=====查询流程sql======" + sql));
        String poolname = CONN_TYPE.workflow.getType();
        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
        rs.executeSql(sql, poolname);
        String tpnr = "";
        if (rs.next()) {
            tpnr = Util.null2String(rs.getString("tpnr"));
        }
       // sql = "select xx from uf_tpnr where id in (" + tpnr + ")";
          sql = "SELECT uf_tpnr.xx AS xx\n" +
                  "FROM e10_common.uf_tpnr\n" +
                  "WHERE uf_tpnr.id IN (" + tpnr + ")\n" +
                  "\tAND uf_tpnr.delete_type = 0\n" +
                  "\tAND uf_tpnr.tenant_key = 'tl2k2r96ty'";
        logger_5b2277ab.info(String.valueOf("=====查询自定义多选sql=====" + sql));
        String poolname_c4 = CONN_TYPE.workflow.getType();
        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
        rs.executeSql(sql, poolname_c4);
        String name = "";
        String flag = "";
        while (rs.next()) {
            name = name + flag + Util.null2String(rs.getString("xx"));
            flag = "+";
        }
        try {
            String tpnrxlk = getSelectValue("tpnrxlk", name, tableName);
          //  sql = "update " + tableName + " set tpnrxlk='" + tpnrxlk + "' where requestid=" + requestid;
            sql = "UPDATE (\n" +
                    "\tSELECT b.*, a.requestid\n" +
                    "\tFROM e10_core_business.wfc_form_data a, e10_core_business."+tableName+" b\n" +
                    "\tWHERE a.dataid = b.form_data_id\n" +
                    "\t\tAND a.delete_type = 0\n" +
                    "\t\tAND a.tenant_key = 'tl2k2r96ty'\n" +
                    ") "+tableName+"\n" +
                    "SET "+tableName+".tpnr = '"+tpnrxlk+"'\n" +
                    "WHERE form_data_id IN (\n" +
                    "\t\tSELECT dataid AS dataid\n" +
                    "\t\tFROM e10_core_business.wfc_form_data\n" +
                    "\t\tWHERE "+tableName+".requestid = '"+requestid+"'\n" +
                    "\t\t\tAND wfc_form_data.delete_type = 0\n" +
                    "\t\t\tAND wfc_form_data.tenant_key = 'tl2k2r96ty'\n" +
                    "\t)\n" +
                    "\tAND "+tableName+".delete_type = 0\n" +
                    "\tAND "+tableName+".tenant_key = 'tl2k2r96ty'";

            String poolname_9s = CONN_TYPE.workflow.getType();
            // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
            rs.executeSql(sql, poolname_9s);
        } catch (Exception e) {
            logger_5b2277ab.info(String.valueOf(e));
            e.printStackTrace();
            return WeaResult.fail(500, StringUtils.isNotBlank(info.getRequestManager().getMessagecontent()) ? info.getRequestManager().getMessagecontent() : "执行失败", getResultMapForAction(weaverResultMap, "result", FAILURE_AND_CONTINUE, info.getRequestManager()));
        }
        return WeaResult.success(getResultMapForAction(weaverResultMap, "result", SUCCESS, info.getRequestManager()));
    }

    /**
     * 获取选择框选项的值
     * @param fieldName 选择框字段名
     * @param fieldValue 选择框字段值
     * @param tableName 选择框字段在的表名
     * @return
     */
    public String getSelectValue(String fieldName, String fieldValue, String tableName) throws Exception {
        String selectValue = "";
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        //String sql = "select selectvalue from workflow_billfield a, workflow_bill b,workflow_selectitem c " + "where a.billid=b.id and c.fieldid=a.id  and b.tablename='" + tableName + "' and a.fieldname='" + fieldName + "' " + "and c.selectname='" + fieldValue + "' and cancel=0";
        String sql ="SELECT c.value_key AS selectvalue\n" +
                "FROM e10_core_business.form_field a, (\n" +
                "\t\tSELECT form.*, form_table.table_name\n" +
                "\t\tFROM e10_core_business.form\n" +
                "\t\t\tLEFT JOIN e10_common.form_table\n" +
                "\t\t\tON form.id = form_table.form_id\n" +
                "\t\t\t\tAND form_table.delete_type = 0\n" +
                "\t\t\t\tAND form_table.tenant_key = 'tl2k2r96ty'\n" +
                "\t\tWHERE form.delete_type = 0\n" +
                "\t\t\tAND form.tenant_key = 'tl2k2r96ty'\n" +
                "\t) b, (\n" +
                "\t\tSELECT t2.id, t2.value_key, t2.name, t3.id AS field_id, t3.delete_type\n" +
                "\t\t\t, t3.tenant_key, t2.archive\n" +
                "\t\tFROM e10_common.formdata_template t, e10_common.formdata_template_details t2, e10_core_business.form_field t3\n" +
                "\t\tWHERE t.id = t2.template_id\n" +
                "\t\t\tAND t3.data_template_id = t.id\n" +
                "\t\t\tAND t.delete_type = 0\n" +
                "\t\t\tAND t.tenant_key = 'tl2k2r96ty'\n" +
                "\t\t\tAND t2.delete_type = 0\n" +
                "\t\t\tAND t2.tenant_key = 'tl2k2r96ty'\n" +
                "\t\tUNION ALL\n" +
                "\t\tSELECT t.id, t.value_key, t.name, t3.id AS field_id, t.delete_type\n" +
                "\t\t\t, t.tenant_key, t.archive\n" +
                "\t\tFROM e10_common.field_option t, e10_core_business.form_field t3\n" +
                "\t\tWHERE t.field_id = t3.id\n" +
                "\t\t\tAND t3.delete_type = 0\n" +
                "\t\t\tAND t3.tenant_key = 'tl2k2r96ty'\n" +
                "\t) c\n" +
                "WHERE a.form_id = b.id\n" +
                "\tAND c.field_id = a.id\n" +
                "\tAND b.table_name = '" + tableName + "'\n" +
                "\tAND a.data_key = '" + fieldName + "'\n" +
                "\tAND c.name = '" + fieldValue + "'\n" +
                "\tAND c.archive = 0\n" +
                "\tAND a.delete_type = 0\n" +
                "\tAND a.tenant_key = 'tl2k2r96ty'\n" +
                "\tAND b.delete_type = 0\n" +
                "\tAND b.tenant_key = 'tl2k2r96ty'\n" +
                "\tAND c.delete_type = 0\n" +
                "\tAND c.tenant_key = 'tl2k2r96ty'";
        logger_5b2277ab.error(String.valueOf("====查询下拉框的id====" + sql));
        String poolname_nj = CONN_TYPE.workflow.getType();
        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
        rs.executeSql(sql, poolname_nj);
        if (rs.next()) {
            selectValue = Util.null2String(rs.getString("selectvalue"));
        }
        return selectValue;
    }

    Map<String, Object> getResultMapForAction(Map<String, Object> map, String key, Object value, RequestManager requestManager) {
        if (key != null && !key.isEmpty()) {
            map.put(key, value);
        }
        String msgContent = requestManager.getMessagecontent();
        if (msgContent != null && !msgContent.isEmpty()) {
            map.put("msgContent", msgContent);
        }
        String msgId = requestManager.getMessageid();
        if (msgId != null && !msgId.isEmpty()) {
            map.put("msgId", msgId);
        }
        return map;
    }
}
